from odoo import models, fields, _, api
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import logging
from odoo.exceptions import UserError
import datetime

logger = logging.getLogger(__name__)


class Info(models.Model):
    _name = 'project.project'
    _inherit = ['project.project', 'mail.thread', 'mail.activity.mixin']
    _description = 'some details of the projects'

    start_datetime = fields.Date(
        string='项目开始时间',
        required=True,
        index=True,
        default=lambda self: fields.Date.today(),
        track_visibility='onchange'
    )
    end_datetime = fields.Date(
        string='预期完成时间',
        required=True,
        index=True,
        track_visibility='onchange'
    )
    url_info = fields.Char(
        '公网信息',
        required=True,
        track_visibility='onchange'
    )
    state = fields.Selection(
        [(1, '项目计划阶段'),
         (2, '项目需求分析阶段'),
         (3, '项目设计阶段'),
         (4, '项目编码阶段'),
         (5, '项目测试阶段'),
         (6, '项目维护阶段'), ],
        string='目前所处阶段', default=1, required=True, track_visibility='onchange')

    state_words = fields.Char(
        string='目前所处阶段',
        compute='_change_word',
        store=True,
        track_visibility='onchange'
    )

    team = fields.Many2one(
        'team.info',
        string='承包项目组',
        required=True,
        track_visibility='onchange'
    )
    user_id = fields.Many2one(
        related='team.person_in_charge_rule',
        string='项目管理员',
        readonly=True,
        required=True
    )
    # website_company_id = fields.Many2one(related='website_id.company_id', string='Website Company', readonly=False)
    helpers_editable = fields.Many2many(
        'person.info',
        string='技术支持人员'
    )
    helpers = fields.Many2many(
        'person.info',
        string='技术支持人员',
        track_visibility='onchange',
        readonly=True
    )
    delta_time = fields.Char(
        '项目总时长(日)',
        compute="count_days",
        default='0',
        store=True,
        track_visibility='onchange'
    )
    delay = fields.Selection(
        [(1, '已延期'),
         (2, '未延期')],
        string='是否延期', default=2, compute='_check_delay', store=True, track_visibility='onchange'
    )

    @api.depends('helpers_editable')
    def _update_helpers(self):
        self.helpers = self.helpers_editable

    @api.onchange('start_datetime', 'end_datetime')
    def check_datetime(self):
        if self.end_datetime:
            curr_date = datetime.datetime.now().strftime('%Y-%m-%d')
            curr_date = datetime.datetime.strptime(curr_date, "%Y-%m-%d")
            curr_date = datetime.datetime.date(curr_date)
            delta_now_start = (self.start_datetime - curr_date).days
            delta_now_end = (self.end_datetime - curr_date).days
            delta_end_start = (self.end_datetime - self.start_datetime).days
            if delta_now_start < 0:
                return {
                    'warning': {
                        'title': "日期提醒",
                        'message': "所设定项目开始时间在今日之前！",
                    },
                }
            if delta_now_end < 0:
                return {
                    'warning': {
                        'title': "日期提醒",
                        'message': "所设定时间会导致项目已延期！",
                    },
                }
            elif delta_end_start < 0:
                # raise UserError(
                #     _('项目截止日期不能在项目开始日期之前！'))
                return {
                    'warning': {
                        'title': "日期提醒",
                        'message': "项目截止日期不能在项目开始日期之前！",
                    },
                }
            elif delta_end_start == 0:
                # raise UserError(
                #     _('项目截止日期不能在项目开始日期之前！'))
                return {
                    'warning': {
                        'title': "日期提醒",
                        'message': "所配置信息要求今日完成",
                    },
                }

    @api.one
    @api.depends('state')
    def _change_word(self):
        if self.state == 1:
            self.state_words = '项目计划阶段'
        if self.state == 2:
            self.state_words = '项目需求分析阶段'
        if self.state == 3:
            self.state_words = '项目设计阶段'
        if self.state == 4:
            self.state_words = '项目编码阶段'
        if self.state == 5:
            self.state_words = '项目测试阶段'
        if self.state == 6:
            self.state_words = '项目维护阶段'

    @api.one
    @api.depends('delta_time')
    def _check_delay(self):
        if self.delta_time == '已延期':
            self.delay = 1
        else:
            self.delay = 2

    def call_owner(self):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the email addr: %s", sender)
        receivers = self.user_id.login  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        mail_msg = """
            <!doctype html>
            <html>
              <head>
                <meta name="viewport" content="width=device-width">
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              </head>
              <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                  <tr>
                    <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                    <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                      <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">
            
                        <!-- START CENTERED WHITE CONTAINER -->
                        <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                        <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">
            
                          <!-- START MAIN CONTENT AREA -->
                          <tr>
                            <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                              <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                <tr>
                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                    <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">尊敬的{owner_name},</p>
                                    <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您的团队{team}所承包的项目,{name}已启动,客户为{partner},要求完成时间{end_datetime},目前剩余交付天数{delta_time}天,请您做好准备！</p>
                                    <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                      <tbody>
                                        <tr>
                                          <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                              <tbody>
                                                <tr>
                                                  <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                </tr>
                                              </tbody>
                                            </table>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                    <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                  </td>
                                </tr>
                              </table>
                            </td>
                          </tr>
            
                        <!-- END MAIN CONTENT AREA -->
                        </table>
            
                        <!-- START FOOTER -->
                        <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                            <tr>
                              <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                              </td>
                            </tr>
                          
                          </table>
                        </div>
                        <!-- END FOOTER -->
            
                      <!-- END CENTERED WHITE CONTAINER -->
                      </div>
                    </td>
                    <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                  </tr>
                </table>
              </body>
            </html>
        """.format(owner_name=self.user_id.name, team=self.team.name, name=self.name, partner=self.partner_id.name,
                   end_datetime=self.end_datetime, delta_time=self.delta_time)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(self.user_id.name, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成为!原因如下:%s') % e)

    def to_next(self):
        if self.state != 6 and self.state != 1:
            self.state = self.state + 1
        elif self.state == 6:
            raise UserError(_('该项目已完结，无法进入下一步！'))
        elif self.state == 1:
            self.call_owner()
            self.state = self.state + 1

    def project_off(self):
        self.unlink()
        return {
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'kanban,form,tree',
            'res_model': 'project.project',
            'target': 'main'
        }

    @api.one
    def call_friend(self, person):
        sender = 'zilouis@qq.com'
        passwd = 'sovlbfcttwogbhhi'
        logger.info("Here is the person: %s", person)
        receiver = person.email  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
        mail_msg = """
                    <!doctype html>
                    <html>
                      <head>
                        <meta name="viewport" content="width=device-width">
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                      </head>
                      <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                        <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                          <tr>
                            <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                            <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                              <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">

                                <!-- START CENTERED WHITE CONTAINER -->
                                <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">

                                  <!-- START MAIN CONTENT AREA -->
                                  <tr>
                                    <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                      <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                        <tr>
                                          <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">尊敬的{person_name},</p>
                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您的团队{team}所承包的项目,{name}已启动,项目负责人为{leader},客户为{partner},要求完成时间{end_datetime},目前剩余交付天数{delta_time}天,请您做好准备！</p>
                                            <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                              <tbody>
                                                <tr>
                                                  <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                      <tbody>
                                                        <tr>
                                                          <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                        </tr>
                                                      </tbody>
                                                    </table>
                                                  </td>
                                                </tr>
                                              </tbody>
                                            </table>
                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                          </td>
                                        </tr>
                                      </table>
                                    </td>
                                  </tr>

                                <!-- END MAIN CONTENT AREA -->
                                </table>

                                <!-- START FOOTER -->
                                <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                    <tr>
                                      <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                        <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                      </td>
                                    </tr>

                                  </table>
                                </div>
                                <!-- END FOOTER -->

                              <!-- END CENTERED WHITE CONTAINER -->
                              </div>
                            </td>
                            <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                          </tr>
                        </table>
                      </body>
                    </html>
                """.format(person_name=person.name, team=self.team.name, name=self.name, leader=self.user_id.name,
                           partner=self.partner_id.name,
                           end_datetime=self.end_datetime, delta_time=self.delta_time)
        message = MIMEText(mail_msg, 'html', 'utf-8')
        message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
        message['To'] = Header(self.user_id.name, 'utf-8')

        subject = '湖北大学瑞翼工坊'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            smtpObj.login(sender, passwd)
            smtpObj.sendmail(sender, receiver, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            raise UserError(_('邮件未发送成为!原因如下:%s') % e)

    @api.one
    def call_friends(self):
        for email in self.team.personnel:
            self.call_friend(email)

    def to_previous(self):
        if self.state != 6 and self.state != 1 and self.state != 2:
            self.state = self.state - 1
        elif self.state == 6:
            raise UserError(_('该项目已完结，无法回退！'))
        elif self.state == 1:
            raise UserError(_('项目计划阶段，无法回退'))
        elif self.state == 2:
            raise UserError(_('项目已正式开始，无法回退'))

    def call_deadline(self):
        deadline = self.end_datetime
        logger.info("deadline: %s", deadline)
        logger.info("deadline type: %s", type(deadline))
        # str转datetime.date  先将str转datetime.datetime, 再转datetime.date
        curr_date = datetime.datetime.now().strftime('%Y-%m-%d')
        curr_date = datetime.datetime.strptime(curr_date, "%Y-%m-%d")
        curr_date = datetime.datetime.date(curr_date)
        # str转datetime.date  先将str转datetime.datetime, 再转datetime.date
        logger.info("curr_date: %s", curr_date)
        logger.info("curr_date type: %s", type(curr_date))
        delta_time = (deadline - curr_date).days
        logger.info("delta_time type: %s", type(delta_time))
        self.delta_time = delta_time
        if delta_time >= 0:
            raise UserError(_('项目交付剩余:%s天') % (delta_time))
        else:
            raise UserError(_('项目已关闭'))

    @api.one
    @api.depends('end_datetime')
    def count_days(self):
        if self.end_datetime:
            for self.delta_time in self:
                deadline = self.end_datetime
                curr_date = datetime.datetime.now().strftime('%Y-%m-%d')
                curr_date = datetime.datetime.strptime(curr_date, "%Y-%m-%d")
                curr_date = datetime.datetime.date(curr_date)
                delta_time = (deadline - curr_date).days
                if delta_time >= 0:
                    self.delta_time = delta_time
                else:
                    self.delta_time = '已延期'

    @api.one
    @api.model
    def auto_count_days(self):
        projects = self.env['project.project'].search([])
        for project in projects:
            if project.end_datetime:
                deadline = project.end_datetime
                curr_date = datetime.datetime.now().strftime('%Y-%m-%d')
                curr_date = datetime.datetime.strptime(curr_date, "%Y-%m-%d")
                curr_date = datetime.datetime.date(curr_date)
                delta_time = (deadline - curr_date).days
                logger.info("delta time is ", delta_time)
                if delta_time >= 0:
                    project.delta_time = delta_time
                    # project.delta_time = delta_time
                else:
                    project.delta_time = '已延期'

    @api.model
    def cry_deadline(self):
        projects = self.env['project.project'].search([])
        for project in projects:
            logger.info("project.delta_time: %s", project.delta_time)
            if project.delta_time != '已延期':
                logger.info("project.delta_time is: %s", project.delta_time)
                delta_time = int(project.delta_time)
                if delta_time <= 3 and delta_time != 0:
                    sender = 'zilouis@qq.com'
                    passwd = 'sovlbfcttwogbhhi'
                    logger.info("Here is the email addr: %s", sender)
                    receivers = project.user_id.login  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
                    mail_msg = """
                                    <!doctype html>
                                    <html>
                                      <head>
                                        <meta name="viewport" content="width=device-width">
                                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                      </head>
                                      <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                                        <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                                          <tr>
                                            <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                            <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                              <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">
        
                                                <!-- START CENTERED WHITE CONTAINER -->
                                                <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                                <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">
        
                                                  <!-- START MAIN CONTENT AREA -->
                                                  <tr>
                                                    <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                                      <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                                        <tr>
                                                          <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">尊敬的{owner_name},</p>
                                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您的团队{team}所承包的项目,{name},要求完成时间{end_datetime},目前剩余交付天数{delta_time}天,剩余时日已不多,请您关注！</p>
                                                            <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                              <tbody>
                                                                <tr>
                                                                  <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                                    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                                      <tbody>
                                                                        <tr>
                                                                          <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                                        </tr>
                                                                      </tbody>
                                                                    </table>
                                                                  </td>
                                                                </tr>
                                                              </tbody>
                                                            </table>
                                                            <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                                          </td>
                                                        </tr>
                                                      </table>
                                                    </td>
                                                  </tr>
        
                                                <!-- END MAIN CONTENT AREA -->
                                                </table>
        
                                                <!-- START FOOTER -->
                                                <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                                  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                                    <tr>
                                                      <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                                        <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                                      </td>
                                                    </tr>
        
                                                  </table>
                                                </div>
                                                <!-- END FOOTER -->
        
                                              <!-- END CENTERED WHITE CONTAINER -->
                                              </div>
                                            </td>
                                            <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                          </tr>
                                        </table>
                                      </body>
                                    </html>
                                """.format(owner_name=project.user_id.name, team=project.team.name, name=project.name,
                                           end_datetime=project.end_datetime, delta_time=project.delta_time)
                    message = MIMEText(mail_msg, 'html', 'utf-8')
                    message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
                    message['To'] = Header(project.user_id.name, 'utf-8')

                    subject = '湖北大学瑞翼工坊'
                    message['Subject'] = Header(subject, 'utf-8')

                    try:
                        smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
                        smtpObj.login(sender, passwd)
                        smtpObj.sendmail(sender, receivers, message.as_string())
                        print("邮件发送成功")
                    except Exception as e:
                        raise UserError(_('邮件未发送成为!原因如下:%s') % e)
                elif delta_time == 0:
                    sender = 'zilouis@qq.com'
                    passwd = 'sovlbfcttwogbhhi'
                    receivers = project.user_id.login  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
                    logger.info("Here is the email receivers: %s", receivers)
                    mail_msg = """
                                <!doctype html>
                                <html>
                                  <head>
                                    <meta name="viewport" content="width=device-width">
                                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                  </head>
                                  <body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
                                    <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
                                      <tr>
                                        <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                        <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
                                          <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">
        
                                            <!-- START CENTERED WHITE CONTAINER -->
                                            <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>
                                            <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">
        
                                              <!-- START MAIN CONTENT AREA -->
                                              <tr>
                                                <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
                                                  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                                    <tr>
                                                      <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
                                                        <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">尊敬的{owner_name},</p>
                                                        <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">您的团队{team}所承包的项目,{name},今日已是最后期限,请您关注！</p>
                                                        <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
                                                          <tbody>
                                                            <tr>
                                                              <td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
                                                                <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
                                                                  <tbody>
                                                                    <tr>
                                                                      <td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; border-radius: 5px; text-align: center;"> <a href="http://admin.hubusugon.cn" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db; border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #3498db;">湖北大学瑞翼工坊内部管理系统</a> </td>
                                                                    </tr>
                                                                  </tbody>
                                                                </table>
                                                              </td>
                                                            </tr>
                                                          </tbody>
                                                        </table>
                                                        <p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">祝您生活愉快！</p>
                                                      </td>
                                                    </tr>
                                                  </table>
                                                </td>
                                              </tr>
        
                                            <!-- END MAIN CONTENT AREA -->
                                            </table>
        
                                            <!-- START FOOTER -->
                                            <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">
                                              <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
                                                <tr>
                                                  <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">
                                                    <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">版权所有©湖北大学瑞翼工坊</span>
                                   eval="[(4, ref('sugon_system.asset_group_user')),(4, ref('sugon_system.asset_group_leader'))]"/>               </td>
                                                </tr>
        
                                              </table>
                                            </div>
                                            <!-- END FOOTER -->
        
                                          <!-- END CENTERED WHITE CONTAINER -->
                                          </div>
                                        </td>
                                        <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">&nbsp;</td>
                                      </tr>
                                    </table>
                                  </body>
                                </html>
                            """.format(owner_name=project.user_id.name,
                                       team=project.team.name,
                                       name=project.name)
                    message = MIMEText(mail_msg, 'html', 'utf-8')
                    message['From'] = Header("湖北大学瑞翼工坊", 'utf-8')
                    message['To'] = Header(project.user_id.name, 'utf-8')

                    logger.info("To: %s", project.user_id.name)

                    subject = '湖北大学瑞翼工坊'
                    message['Subject'] = Header(subject, 'utf-8')

                    try:
                        smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
                        smtpObj.login(sender, passwd)
                        smtpObj.sendmail(sender, receivers, message.as_string())
                        print("邮件发送成功")
                    except Exception as e:
                        raise UserError(_('邮件未发送成为!原因如下:%s') % e)
                else:
                    break
